package com.opNaga.bpmn.panel;

import com.opNaga.bpmn.palettes.WorkflowPalette;
import com.yworks.yfiles.bpmn.view.BpmnPropertyPanel;
import com.yworks.yfiles.bpmn.view.BpmnRealizerFactory;
import com.yworks.yfiles.bpmn.view.BpmnTypeEnum;
import com.yworks.yfiles.bpmn.view.BpmnView;
import y.base.Graph;
import y.base.GraphEvent;
import y.base.GraphListener;
import y.base.Node;
import y.io.GraphMLIOHandler;
import y.io.IOHandler;
import y.util.D;
import y.view.*;
import y.view.hierarchy.HierarchyEvent;
import y.view.hierarchy.HierarchyListener;
import y.view.hierarchy.HierarchyManager;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

/**
 * User: Junyi BEI
 * Date: 20/03/2012
 * Time: 17:56
 */
public class WorkFlowPanel extends JSplitPane implements BPMPPanel {
    private Graph2DUndoManager undoManager;
    private Graph2DClipboard clipboard;
    private Graph2DView view;

    public WorkFlowPanel() {
        view = new BpmnView();
        view.setFitContentOnResize(true);
        new HierarchyManager(view.getGraph2D());

        JPanel titledOverview = PanelSupport.createTitledPanel(PanelSupport.createOverview(view, new Dimension(new Double(0.2 * getWidth()).intValue(), 200)), "Overview");
        JPanel titledPalette = PanelSupport.createTitledPanel(new WorkflowPalette(view), "BPMN Palette");
        JSplitPane overviewPaletteSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, titledOverview, titledPalette);
        overviewPaletteSplit.setResizeWeight(0.4);
        JPanel titledProperties = PanelSupport.createTitledPanel(new BpmnPropertyPanel(view), "Properties");
        JSplitPane overviewPalettePropertiesSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, overviewPaletteSplit, titledProperties);
        overviewPalettePropertiesSplit.setResizeWeight(0.6);
        setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        setLeftComponent(overviewPalettePropertiesSplit);
        setRightComponent(view);

        // set up clipboard
        clipboard = new Graph2DClipboard(view);
        clipboard.setCopyFactory(view.getGraph2D().getGraphCopyFactory());

        // set up undo manager
        undoManager = new Graph2DUndoManager(view.getGraph2D());
        undoManager.setViewContainer(view);
    }

    public Graph2DClipboard getClipboard() {
        return clipboard;
    }

    public Graph2DUndoManager getUndoManager() {
        return undoManager;
    }

    public Graph2DView getView() {
        return view;
    }

    public void loadGraph(InputStream inputStream) {
        try {
            IOHandler ioh = new GraphMLIOHandler();
            view.getGraph2D().clear();
            ioh.read(view.getGraph2D(), inputStream);
        } catch (IOException e) {
            D.bug(e);
        }
        view.fitContent();
        view.updateView();
    }

    public void saveGraph(OutputStream outputStream) {
        IOHandler ioh = new GraphMLIOHandler();
        try {
            ioh.write(view.getGraph2D(), outputStream);
        } catch (IOException ioe) {
            D.show(ioe);
        }
    }
}
